package Beans;

import dao.UserDao;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;
import org.icefaces.apache.commons.io.FileUtils;
import org.joda.time.LocalDate;

/**
 @author chuck
 */
public class User {

  public final static String DEFAULT_PROFILE_PICTURE = "noProfilePhoto.png";
  //Short user
  private int userID;
  private String username;
  private String sex;
  //Full user
  private String email;
  private String profilePicture;
  private String password;
  private String lookingForSex;
  private int age;
  private String city;
  private String goal;
  private int height;
  private String resume;
  private String apparence;
  private Date lastLogin;
  private int nbVotes;
  private double score;

  private Boolean isWarnedByEmail;
  private Boolean isWarnedAddWatchList;
  private Boolean isPrivileged = false;

  private List<User> favorites;
  private List<String> pictures;
  private List<String> interestSelected;
  private final String PICTURE_SAVE_PATH = "C:\\awesome-dates\\userPictures\\";

  public User() {
  }

  //Fill what is needed
  public User(int userID, String username) {
    this.username = username;
    this.userID = userID;
  }

  public int getUserID() {
    return userID;
  }

  public void setUserID(int userID) {
    this.userID = userID;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public String getLookingForSex() {
    return lookingForSex;
  }

  public void setLookingForSex(String lookingForSex) {
    this.lookingForSex = lookingForSex;
  }

  public int getAge() {
    return (new LocalDate().getYear() - age - 1);
  }

  public void setAge(int annee) {
    this.age = annee;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getGoal() {
    return goal;
  }

  public void setGoal(String goal) {
    this.goal = goal;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public String getResume() {
    return resume;
  }

  public void setResume(String resume) {
    this.resume = resume;
  }

  public String getApparence() {
    return apparence;
  }

  public void setApparence(String apparence) {
    this.apparence = apparence;
  }

  public Date getLastLogin() {
    return lastLogin;
  }

  public void setLastLogin(Date lastLogin) {
    this.lastLogin = lastLogin;
  }

  public int getNbVotes() {
    return nbVotes;
  }

  public void setNbVotes(int nbVotes) {
    this.nbVotes = nbVotes;
  }

  public double getScore() {
    return score;
  }

  public void setScore(double score) {
    this.score = score;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getProfilePicture() {
    return (profilePicture == null ? DEFAULT_PROFILE_PICTURE : profilePicture);
  }

  public void setProfilePicture(String profilePicture) {
    this.profilePicture = (profilePicture == null ? DEFAULT_PROFILE_PICTURE : profilePicture);
  }

  public Boolean getIsWarnedByEmail() {
    return isWarnedByEmail;
  }

  public void setIsWarnedByEmail(Boolean isWarnedByEmail) {
    this.isWarnedByEmail = isWarnedByEmail;
  }

  public Boolean getIsWarnedAddWatchList() {
    return isWarnedAddWatchList;
  }

  public void setIsWarnedAddWatchList(Boolean isWarnedAddWatchList) {
    this.isWarnedAddWatchList = isWarnedAddWatchList;
  }

  public Boolean getIsPrivileged() {
    return isPrivileged;
  }

  public void setIsPrivileged(Boolean isPrivileged) {
    this.isPrivileged = isPrivileged;
  }

  public List<User> getFavorites() {
    return favorites;
  }

  public void setFavorites(List<User> favorites) {
    this.favorites = favorites;
  }

  public List<String> getPictures() {
    if (pictures == null || pictures.isEmpty()) {
      loadPictures();
    }
    return pictures;
  }

  public void loadPictures() {
    pictures = UserDao.getPictures(userID);
  }

  public void setPictures(List<String> pictures) {
    this.pictures = pictures;
  }

  public List<String> getInterest() {
    return interestSelected;
  }

  public void setInterest(List<String> interestSelected) {
    this.interestSelected = interestSelected;
  }

  public void uploadListener(FileEntryEvent e) {
    try {
      FileEntry fe = (FileEntry) e.getComponent();
      FileEntryResults results = fe.getResults();
      File parent = null;

      for (FileEntryResults.FileInfo i : results.getFiles()) {
        if (i.isSaved()) {
          File file = i.getFile();
          if (file != null) {
            parent = file.getParentFile();
            makeDir();
            Files.deleteIfExists(new File(PICTURE_SAVE_PATH + username + "\\" + file.getName()).toPath());
            if (file.renameTo(new File(PICTURE_SAVE_PATH + username + "\\" + file.getName()))) {
              UserDao.insertPicture(userID, file.getName());
            }
          }
        }
      }
      if (parent != null) {
        FileUtils.deleteDirectory(parent.getAbsoluteFile());
      }
    } catch (IOException ex) {
      Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
    }
    loadPictures();
  }

  public void changeProfilePic(FileEntryEvent e) {
    try {
      FileEntry fe = (FileEntry) e.getComponent();
      FileEntryResults results = fe.getResults();
      File parent = null;

      for (FileEntryResults.FileInfo i : results.getFiles()) {
        if (i.isSaved()) {
          File file = i.getFile();
          if (file != null) {
            parent = file.getParentFile();
            makeDir();
            Files.deleteIfExists(new File(PICTURE_SAVE_PATH + username + "\\" + file.getName()).toPath());
            if (file.renameTo(new File(PICTURE_SAVE_PATH + username + "\\" + file.getName()))) {
              UserDao.changeProfilePicture(userID, file.getName());
              profilePicture = file.getName();
            }
          }
        }
      }
      if (parent != null) {
        FileUtils.deleteDirectory(parent.getAbsoluteFile());
      }
    } catch (IOException ex) {
      Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  private void makeDir() {
    File file = new File("C:\\awesome-dates\\userPictures\\" + username);
    if (!file.exists()) {
      if (file.mkdirs()) {
        System.out.println("DEBUG: Directory is created!");
      } else {
        System.out.println("DEBUG: Failed to create directory!");
      }
    }
  }
}
